package com.tankgame.models;

import com.tankgame.utils.Constants;
import com.tankgame.utils.Directions;

import java.util.Random;

public class EnemyTank extends BaseTank implements Runnable {
    public EnemyTank(int x, int y) {
        super(x, y, Constants.ENEMY_TANK_COLOR);
    }

    @Override
    public void run() {
        Random random = new Random();
        while (isAlive) {
            // 随机设置坦克方向
            Directions[] directions = Directions.values();
            setDirection(directions[random.nextInt(directions.length)]);

            // 在当前方向上移动一定的步数
            int steps = random.nextInt(30) + 20; // 随机移动20到50步
            for (int i = 0; i < steps; i++) {
                move();

                try {
                    Thread.sleep(50); // 控制移动速度
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            // 自动开火
            fire();

            // 休眠一段时间后再改变方向
            try {
                Thread.sleep(1000); // 等待1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}